using System;
using System.Collections.Generic;
using FPLibrary;
using FrameOperateSync;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEditor.SceneTemplate;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityGameFramework.Runtime;
using Behaviour = FrameOperateSync.Behaviour;

namespace Pvp.Stage
{
    public static class StageBoxProcess
    {
        [MenuItem("Tools/Stage/processActiveScene")]
        static void ProcessActiveScene()
        {
            var scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();

            var stageData = ScriptableObject.CreateInstance<StageLogicData>();

            ProcessStage(stageData, scene);

            var path = System.IO.Path.Combine("Assets", "WarIIStage.asset");
            AssetDatabase.CreateAsset(stageData, path);
        }
        
        public static void ProcessStage(StageLogicData stageData, Scene scene)
        {
            List<BoxCollider> boxList = new List<BoxCollider>();

            var roots = scene.GetRootGameObjects();
            foreach (var go in roots)
            {
                var boxes = go.GetComponentsInChildren<BoxCollider>(true);
                if (boxes != null && boxes.Length > 0)
                {
                    boxList.AddRange(boxes);
                }
            }
            
            foreach (var box in boxList)
            {
                StageBoxItem boxItem = new StageBoxItem();
                boxItem.boxName = box.gameObject.name;
                boxItem.isInitActive = box.gameObject.activeInHierarchy;
                boxItem.box = FPBB.ToFPBB(box.bounds);
                boxItem.rotation = FPVector.ToFPVector(box.transform.eulerAngles);
                
                var meshFilter = box.gameObject.GetComponent<MeshFilter>();
                if (meshFilter != null && meshFilter.sharedMesh != null)
                {
                    boxItem.facadeResPath = AssetDatabase.GetAssetPath(meshFilter.sharedMesh);
                }
                
                stageData.stageBox.Add(boxItem);
            }
        }


        [MenuItem("Tools/Stage/ttt")]
        static void BuildLogicStage()
        {
            var stage = AssetDatabase.LoadAssetAtPath<StageLogicData>("Assets/WarIIStage.asset");
            var scene = CreateStage(stage);
            
            

        //    AssetDatabase.CreateAsset(scene,"testScene.unity");
        }

        public static Scene CreateStage(StageLogicData stageLogicData)
        {
            if (stageLogicData == null)
                throw new Exception("CreateStage error 0");
            
            var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);

            var logicGo = StageBuildProcess.CreateStageRoot();
            if (stageLogicData.stageBox != null)
            {
                foreach (var box in stageLogicData.stageBox)
                {
                    StageBuildProcess.NewBoxObject(box);
                }
            }

            UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(logicGo, scene);

            return scene;
        }
    }

    public static class StageBuildProcess
    {
        private static Transform _boxRoot;
        private static Transform _gearRoot;
        private static Transform _monsterRoot;
        private static Transform _npcRoot;
        private static Transform _birthPointRoot;
        private static Transform _resGatherPointRoot;
        
        public static GameObject CreateStageRoot()
        {
            var root = new GameObject("__stageRoot").transform;
            _boxRoot = NewGameObject("_box",root); 
            _gearRoot = NewGameObject("_gear",root);
            _monsterRoot = NewGameObject("_monster",root);
            _npcRoot = NewGameObject("_npc",root);
            _birthPointRoot = NewGameObject("_birthPoint",root);
            _resGatherPointRoot = NewGameObject("_resGather",root);

            return root.gameObject;
        }

        public static void NewBoxObject(StageBoxItem boxItem)
        {
            GameObject tmp = GameObject.CreatePrimitive(PrimitiveType.Cube);
            tmp.name = boxItem.boxName;
            tmp.transform.SetParent(_boxRoot);
            tmp.transform.position = boxItem.box.Center.ToVector();
            tmp.transform.eulerAngles = boxItem.rotation.ToVector();
            tmp.transform.localScale = boxItem.box.Size.ToVector();
            var box = tmp.AddComponent<BoxCollider>();
            box.center = Vector3.zero;
            box.size = Vector3.one;
        }

        static Transform NewGameObject(string name, Transform root)
        {
            GameObject tmp = new GameObject(name);
            tmp.transform.SetParent(root);
            tmp.transform.localPosition = Vector3.zero;
            tmp.transform.localScale = Vector3.one;
            return tmp.transform;
        }
    }
}